package pers.cz.mock;

import com.alibaba.fastjson.JSON;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @program: postgirl-collection-client
 * @description: DrqueryMock
 * @author: Cheng Zhi
 * @create: 2022-05-18 22:36
 **/
public class DrqueryMock {

    public static void begin(HttpServletRequest request, ModelAndView object) {

        System.out.println("调用进来饿了");
        System.out.println("----这是录制到的入参----" + formatParam2JsonObj(request));
        System.out.println("----这是录制到的出参----" + JSON.toJSON(object.getModelMap()));
    }


    protected static String formatParam2JsonObj(HttpServletRequest request) {
        BufferedReader in = null;
        String encoding=request.getCharacterEncoding();
        if(encoding==null||encoding.equals(""))encoding="UTF-8";
        try {
            in = new BufferedReader(new InputStreamReader(request.getInputStream(), encoding));
            String jsonParam="";
            String line;
            while ((line = in.readLine()) != null) {
                jsonParam +=line;
            }
            return JSON.toJSON(jsonParam).toString();
        }
        catch (Exception e) {
            System.out.println("read parameter from body failed");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    System.out.println("close stream failed");
                }
            }
        }
        return null;
    }
}
